<?php

namespace model;

use common\Model;

class admin_group
{
	const CACHENAME = "admin_group_";
	protected $model = null;
	protected $TABLE = "admin_group", $TABLE_T = "admin_user";
	function __construct()
	{
		$this->model = new model();
	}

	//查找一条数据
	public function findData($gid = null)
	{
		$whereid = $gid ?? $_GET['id'];
		$data = GetCxuuCache(self::CACHENAME . $whereid);
		if (empty($data)) {
			$data = $this->model->db($this->TABLE)->where(['id' => $whereid])->find();
			SetCxuuCache(self::CACHENAME . $whereid, $data); //永久保存
		}
		return $data;
	}

	//查询列表
	public function selectGroup()
	{
		return $this->model->db($this->TABLE)->select();
	}

	//获取数据并分页
	public function jsonPageSelect()
	{
		$p = intval($_GET['page'] ?? 0) ?: 1;
		$num = intval($_GET['limit'] ?? 0) ?: 10;

		$page = ['num' => $num, 'p' => $p, 'return' => true];
		return [
			'data' => $this->model->db($this->TABLE)->Page($page)->order('id DESC')->select(),
			'page' => $this->model->db($this->TABLE)->getPage()
		];
	}

	//判断用户组名是否存在
	private function checkNameData()
	{
		if (empty($_POST['id'])) {
			$where['groupname'] = $_POST['groupname'];
		} else {
			//验证用户组名是否存在 查询不等当前ID的数据进行比对
			$where = [
				'id <>' => $_POST['id'],
				'groupname' => $_POST['groupname'],
			];
		}
		return $this->model->db($this->TABLE)->where($where)->find();
	}
	//添加数据
	public function insertData()
	{
		$groupname = VarFilter($_POST['groupname']);
		//验证用户组名是否存在
		if (!empty($this->checkNameData()))
			return ['status' => 0, 'msg' => '用户组名已经存在！'];
		if (empty($groupname))
			return ['status' => 0, 'msg' => '请填写用户组名'];
		$data = [
			'groupname' => $groupname,
			'systemrole' => $_POST['systemrole'],
		];
		$re_key = $this->model->db($this->TABLE)->insert($data);
		if ($re_key) {
			SqlLog("添加角色: {$groupname}");
			CleanDbCache($this->TABLE);
			return ['status' => 1, 'msg' => '添加成功'];
		} else {
			return ['status' => 0, 'msg' => '数据错误！添加失败'];
		}
	}

	//更新数据
	public function updateData()
	{
		$groupname = VarFilter($_POST['groupname']);

		//验证用户组名是否存在
		if (!empty($this->checkNameData($groupname)))
			return ['status' => 0, 'msg' => '用户组名已经存在！'];
		if (empty($groupname))
			return ['status' => 0, 'msg' => '请填写用户组名'];

		$whereid = $_POST['id'];
		$data = [
			'groupname' => $groupname,
			'systemrole' => $_POST['systemrole'],
		];
		$re_key = $this->model->db($this->TABLE)->Where(['id' => $whereid])->Update($data);
		if ($re_key > 0) {
			//写入日志
			SqlLog("修改角色及权限:{$groupname} ID:{$whereid}");
			DelCxuuCache(self::CACHENAME . $whereid); //删除对应缓存
			return ['status' => 1, 'msg' => '修改成功'];
		} else {
			return ['status' => 0, 'msg' => '数据没有变化'];
		}
	}

	//更新数据
	public function updateChannlroleData()
	{
		$whereid = $_POST['id'];
		$data = [
			'channlrole' => JsonEncode($_POST['channlrole']),
		];
		$re_key = $this->model->db($this->TABLE)->Where(['id' => $whereid])->Update($data);
		if ($re_key > 0) {
			SqlLog("修改角色栏目权限 ID:{$whereid}");
			DelCxuuCache(self::CACHENAME . $whereid); //删除对应缓存
			return ['status' => 1, 'msg' => '修改成功'];
		} else {
			return ['status' => 0, 'msg' => '数据没有变化'];
		}
	}

	//删除一条数据
	public function deleteOneData()
	{
		$whereid = $_POST['id'];
		$find = $this->model->db($this->TABLE_T)->where(['gid' => $whereid])->find();
		if ($find) {
			return false; //如该角色下有管理员，拒绝删除角色
		} else {
			SqlLog("删除角色 ID:" . $whereid); //写入日志
			DelCxuuCache(self::CACHENAME . $whereid); //删除对应缓存
			return $this->model->db($this->TABLE)->where(['id' => $whereid])->Delete();
		}
	}
}
